<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>
<script type="text/javascript">
    $().ready(function(){

        /**
         * Create the auto-complete form for TAGS
         * 
         */
        function split(val) {
            return val.split(/,\s*/);
        }
        function extractLast(term) {
            return split(term).pop();
        }

        $("#collection_item_tagString").expandingTextArea();
        $("#collection_item_collectionName").expandingTextArea();

        $("#collection_item_tagString").autocomplete({
            source: function(request, response) {
                $.getJSON('<?php echo url_for('SubjectTagSearch') ?>', {
                    term: extractLast(request.term)
                }, response);
            },
            search: function() {
                // custom minLength
                var term = extractLast(this.value);
                if (term.length < 2) {
                    return false;
                }
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function(event, ui) {
                var terms = split( this.value );
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push( ui.item.value );
                // add placeholder to get the comma-and-space at the end
                terms.push("");
                this.value = terms.join(", ");
                return false;
            }
        });

        /*       $(".relatedItemName").keyup(function (){
            $(this).parent().parent().find('.relatedItemUin').val('');
        });
         */

        $(".relatedItemName").autocomplete({
            minLength: 2,
            source: function(request, response) {
                $.getJSON('<?php echo url_for('CollectionNameSearch') ?>', {
                    term: extractLast(request.term)
                }, response);
            },
            select: function(event, ui) {
                $(this).val(ui.item.label);
                $(this).parent().parent().find('.relatedItemUin').val(ui.item.value);
                return false;

            }
        });

        $(".propertyValue").expandingTextArea();

        $(".propertyValue").each(function(){
            $(this).textareaCount({
                'maxCharacterSize': 240,
                'warningNumber'   : 10,
                'originalStyle'   : 'characterCounter',
                'warningStyle'   : 'characterCounterWarning',
                'displayFormat'  : '#left / #max Characters Left'
            });
        });

        $(".dateSelect").datepicker({
            changeMonth: true,
            changeYear: true,
            showOn: "both"


        });




    });
</script>
<form class="collection-edit-form" action="<?php echo url_for('Collection' . ($form->getObject()->isNew() ? 'Create' : 'Update'), array('uin' => ($form->getObject()->isNew() ? '' : $form->getObject()->getUin()))) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
    <?php if (!$form->getObject()->isNew()): ?>
        <input type="hidden" name="sf_method" value="put" />
    <?php endif; ?>
    <?php echo $form->renderHiddenFields(false) ?>
        <div class="form-block details-form-block">
            <h2>Details</h2>
            <table class="prop-edit-listing">
            <?php echo $form['collectionName']->renderRow() ?>
            <?php echo $form['collectionTypeUin']->renderRow() ?>
            <?php echo $form['visibilityLevelUin']->renderRow() ?>
            <?php echo $form['tagString']->renderRow() ?>
        </table>
    </div>
    <div class="form-block properties-form-block">
        <h2>Properties</h2>
        <table class="prop-edit-listing">
            <thead>
                <tr>
                    <td>Property</td>
                    <td>Value</td>
                    <td>Delete</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <?php echo $form['applicableStart']->renderLabel() ?>
                    </td>
                    <td>
                        <?php echo $form['applicableStart']->renderError() ?>
                        <?php echo $form['applicableStart']->render() ?>
                    </td>
                </tr>

                <tr>
                    <td>
                        <?php echo $form['applicableEnd']->renderLabel() ?>
                    </td>
                    <td>
                         <?php echo $form['applicableEnd']->renderError() ?>
                        <?php echo $form['applicableEnd']->render() ?>
                    </td>
                </tr>
                    <?php foreach ($form['CollectionProperties'] as $propertyForm): ?>
                <?php echo $propertyForm ?>
                <?php endforeach; ?>
                <?php foreach ($form['NewCollectionProperty'] as $propertyForm): ?>
                <?php echo $propertyForm ?>
                <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <div class="form-block relations-form-block">
                <h2>Related Collections</h2>
                <table class="prop-edit-listing">
                    <thead>
                        <tr>
                            <td>Type</td>
                            <td>Related Collection</td>
                            <td>Delete</td>
                        </tr>
                    </thead>
                    <tbody>
                <?php foreach ($form['CollectionRelations'] as $relationForm): ?>
                <?php echo $relationForm ?>
                <?php endforeach; ?>
                <?php foreach ($form['NewCollectionRelation'] as $relationForm): ?>
                <?php echo $relationForm ?>
                <?php endforeach; ?>
                <?php foreach ($form->getObject()->getRelatingCollections() as $relation): ?>
                                    <tr>
                                        <td>
                                            <a href="<?php echo url_for('CollectionShow', array('uin' => $relation->getThisItem()->getUin())) ?>">
                            <?php echo $relation->getThisItem() ?>
                                </a>
                            </td>
                            <td><?php echo $relation->getRelationType() ?></td>
                            <td>This item</td>
                        </tr>
                <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
    <?php if (sfConfig::get('app_collection_enable_resources')): ?>

                                        <div class="form-block resources-form-block">
                                            <h2>Resources</h2>
                                            <table class="prop-edit-listing">
                                                <thead>
                                                    <tr>
                                                        <td>Name</td>
                                                        <td>Upload File</td>
                                                        <td>Delete</td>
                                                    </tr>
                                                </thead>
                                                <tbody>
                <?php foreach ($form['CollectionResources'] as $resourceForm): ?>

                                            <tr>
                                                <td><?php echo $resourceForm['resourceName']->render() ?></td>
                                                <td>&nbsp;</td>
                                                <td><?php echo $resourceForm['resourceDelete']->render() ?><?php echo $resourceForm->renderHiddenFields(true); ?></td>
                                            </tr>
                <?php endforeach; ?>
                <?php foreach ($form['NewCollectionResource'] as $resourceForm): ?>

                                                <tr>
                                                    <td><?php echo $resourceForm['resourceName']->render() ?></td>
                                                    <td><?php echo $resourceForm['resourceLink']->render() ?></td>
                                                    <td>&nbsp;<?php echo $resourceForm->renderHiddenFields(true); ?></td>
                                                </tr>
                <?php endforeach; ?>
                                            </tbody>
                                        </table>
                                    </div>
    <?php endif; ?>
                                                <div class="context-actions">
                                                    <ul>
            <?php if (!$form->getObject()->isNew()): ?>
                                                    <li>
                                                        <a class="context-action" href="<?php echo url_for('CollectionDelete', array('uin' => $form->getObject()->getUin())) ?>" id="delete">Delete Collection</a>
                                                    </li>
                                                    <li>
                                                        <a class="context-action" href="<?php echo url_for('CollectionShow', array('uin' => $form->getObject()->getUin())) ?>">Cancel Edits</a>
                                                    </li>
                                                    <li>
                                                        <input class="context-action" type="submit" value="Apply" id="apply" name="apply" />
                                                    </li>
            <?php else: ?>
                                                        <li>
                                                            <input class="context-action" type="submit" value="Save and New" id="saveAndNew" name="saveAndNew" />
                                                        </li>
            <?php endif; ?>

            <li>
                <input class="context-action" type="submit" value="Save" id="save" name="save" />
            </li>
        </ul>
    </div>
</form>